<?php
class NotificationsController extends AppController {
	public $name = 'Notifications';

	function beforeFilter() {
		$this->Auth->allow('unsubscribe');
		$this->Auth->authorize = 'controller';
		$this->Security->blackHoleCallback = 'blackhole';
	}

	function blackhole($type) {
		throw new MethodNotAllowedException(__('Nope: ' . $type . ' - ' . Router::url($this->here, true)));
	}

	public function isAuthorized($user) {
		return (AuthComponent::User('role') >= '1') ? true : false;
	}

	public function index(){

	}

	public function unsubscribe($userId = null, $token = null){
		$notificationData = $this->Notification->find('all', array('conditions' => array('Notification.user_id' => $userId, 'Notification.token' => $token)));

		if(count($notificationData) > 0){
			if($this->Notification->delete($notificationData[0][$this->Notification->alias]['id'])){
				$this->Session->setFlash('You will no longer receive email alerts for this thread', 'default', array('class' => 'success'));
				$this->redirect($this->referer());
			}
		}
		$this->redirect($this->referer());
	}

}